package com.will.netty.phaseITest;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;

/**
 * @author Will
 * @description:
 *              双向通信练习  -   服务端
 * @date 2023-06-28 22:09
 * @version: 1.0
 */
@Slf4j
public class EchoServer {
    public static void main(String[] args) {
        new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel channel) throws Exception {
                        log.debug("server init...");
                        ChannelPipeline pipeline = channel.pipeline();
                        pipeline.addLast(new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.debug(msg.toString());
                                ByteBuf byteBuf = (ByteBuf) msg;
                                String string = byteBuf.toString(Charset.defaultCharset());

                                // 释放 byteBuf
                                byteBuf.release();

                                log.debug("服务端接收到信息：{}", string);
                                Student student = new Student(string);
                                super.channelRead(ctx, student);
                            }
                        });
                        pipeline.addLast(new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                // 向客户端发送信息
                                ctx.channel().writeAndFlush("hi, " + msg);
                            }
                        });
                        // 需要转成 ByteBuf 传输
                        pipeline.addLast(new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                ByteBuf byteBuf = ctx.alloc().buffer();
                                byteBuf.writeBytes(msg.toString().getBytes());
                                ctx.write(byteBuf);
                            }
                        });
                    }
                })
                .bind(8080);
    }
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static
    class Student {
        private String name;
    }
}
